﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using RpgLibrary;
using RpgLibrary.WorldClasses;
using RpgLibrary.CharacterClasses;

using XRpgLibrary.TileEngine;
using XRpgLibrary.ExtensionMethods;
namespace XLevelEditor
{
    public partial class FormNewLayer : Form
    {
        #region Field Region
        int LayerWidth;
        int LayerHeight;
        MapLayerData layerData;
        List<Tileset> tilesets;
        #endregion
        
        #region Property Region

        public MapLayerData LayerData
        {
            get { return layerData; }
        }
        
        #endregion
        
        #region Constructor Region
        
        public FormNewLayer(int width, int height, List<Tileset> tilesets, ComboBox.ObjectCollection tilesetItems)
        {
            InitializeComponent();

            LayerWidth = width;
            LayerHeight = height;
            this.tilesets = tilesets;

            foreach(object item in tilesetItems)
                cbTileset.Items.Add(item);

            cbTileset.SelectedIndex = 0;

            pbTilePreview.Height = 64;
            pbTilePreview.Width = 64;

            btnOK.Click += new EventHandler(btnOK_Click);
        }
        
        #endregion
        
        #region Button Event Handler Region

        void btnOK_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(tbLayerName.Text))
            {
                MessageBox.Show("You must enter a name for the layer.", "Missing Layer Name");
                return;
            }

            BuildNewLayer();

            this.DialogResult = DialogResult.OK;

            this.Close();
        }

        private void BuildNewLayer()
        {
            int tileIndex = -1;
            int tilesetIndex = -1;

            if (cbFillLayer.Checked)
            {
                tileIndex = (int)nudTileIndex.Value;
                tilesetIndex = cbTileset.SelectedIndex;
            }

            layerData = new MapLayerData(
                tbLayerName.Text,
                LayerWidth,
                LayerHeight,
                true,
                tileIndex,
                tilesetIndex,
                new List<CharacterData>());
        }

        #endregion

               void pbTilePreview_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
                {
                    Image image = tilesets[cbTileset.SelectedIndex].Texture.ToImage();

                    int acrossX = image.Width / Engine.TileWidth;
                    int acrossY = image.Height / Engine.TileHeight;

                    int y = (int)nudTileIndex.Value / acrossX;
                    int x = (int)nudTileIndex.Value % acrossX;

                    e.Graphics.InterpolationMode = 
                        System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                    e.Graphics.SmoothingMode = 
                        System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                    e.Graphics.DrawRectangle(
                        new Pen(Color.White),
                        new Rectangle(0,0, pbTilePreview.Width, pbTilePreview.Height));
                    e.Graphics.DrawImage(
                        image, 
                        //ClientRectangle,

                        new System.Drawing.Rectangle(
                            (pbTilePreview.Width / 2) - (Engine.TileWidth / 2),
                            (pbTilePreview.Height / 2) - (Engine.TileHeight / 2),
                            Engine.TileWidth,
                            Engine.TileHeight),

                        new System.Drawing.Rectangle(
                            x * Engine.TileWidth, 
                            y * Engine.TileHeight, 
                            Engine.TileWidth, 
                            Engine.TileHeight), 
                        GraphicsUnit.Pixel);
                    

                }

               private void nudTileIndex_ValueChanged(object sender, EventArgs e)
               {
                   pbTilePreview.Refresh();
               }

    }
}